
//
//  LimitTableViewController.m
//  SP2P
//
//  Created by 吴建东 on 16/6/12.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "LimitTableViewController.h"
#import "LimitTableCell.h"
#import "BankTableCell.h"
@interface LimitTableViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_dataArray;
    NSMutableArray *_imageArray;
    NSMutableArray *_limitArray;
    NSMutableArray *_dayLimitArray;
    NSMutableArray *_isLowArray;
    NSMutableArray *_bankCodeArray;
    NSMutableArray *_isOpenBankArray;

}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation LimitTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initNavBarTitle:@"选择银行" andLeftItemName:@"返回" andRightItemName:@""];
    
    [self initData];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initData {
    _dataArray = [[NSMutableArray alloc] initWithArray:@[@"工商银行",@"建设银行",@"农业银行",@"中国银行",@"民生银行",@"光大银行",@"兴业银行",@"平安银行"]]; //,@"交通银行",@"邮政银行",@"中信银行",@"广发银行",@"浦发银行",@"北京银行",@"上海银行"
    _imageArray = [[NSMutableArray alloc] initWithArray:@[@"BankLogo_gsyh",@"BankLogo_jsyh",@"BankLogo_nyyh",@"BankLogo_zgyh",@"BankLogo_msyh",@"BankLogo_gdyh",@"BankLogo_xyyh",@"BankLogo_payh"]]; //,@"BankLogo_jtyh",@"BankLogo_yzyh",@"BankLogo_zxyh",@"BankLogo_gfyh",@"BankLogo_pfyh",@"BankLogo_bjyh",@"BankLogo_shyh"
    _limitArray = [[NSMutableArray alloc] initWithArray:@[@"50000",@"20000",@"20000",@"50000",@"50000",@"50000",@"50000",@"50000"]]; //,@"50000",@"50000",@"5000",@"5000",@"5000"
    _dayLimitArray = [[NSMutableArray alloc] initWithArray:@[@"50000",@"20000",@"20000",@"100000",@"1000000",@"1000000",@"50000",@"1000000"]]; //,@"1000000",@"1000000",@"5000",@"5000",@"5000"
    _isLowArray = [[NSMutableArray alloc] initWithArray:@[@"",@"",@"",@"",@"",@"",@"",@""]]; //,@"",@"2",@"",@"",@""
    _isOpenBankArray = [[NSMutableArray alloc] initWithArray:@[@"",@"",@"",@"",@"",@"",@"",@""]]; //,@"",@"",@"",@"",@""
//    _bankCodeArray = [[NSMutableArray alloc] initWithArray:@[@"102",@"105",@"301",@"305",@"303",@"302",@"309",@"410",@"104",@"103",@"401",@"310",@"403"]]; //
    _bankCodeArray = [[NSMutableArray alloc] initWithArray:@[@"ICBC",@"CCB",@"ABC",@"BOC",@"CMBC",@"CEB",@"CIB",@"SZPAB"]]; //,@"BCOM",@"PSBC",@"CITIC",@"GDB",@"SPDB",@"BCCB",@"BOS"
}

#pragma mark - TableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_dataArray.count) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(cellSelectWithBankName:bankCode:)]) {
            [self.delegate cellSelectWithBankName:_dataArray[indexPath.row] bankCode:_bankCodeArray[indexPath.row]];
            
            [self back];
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count>0?_dataArray.count:1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_dataArray.count) {
        static NSString *cellID = @"cellID";
        BankTableCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellID];
            if (cell == nil) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"BankTableCell" owner:self options:nil][0];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            NSString *bankNameStr = _dataArray[indexPath.row];
            cell.bankNameLabel.text = bankNameStr;
            
            if ([bankNameStr rangeOfString:@"建设银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_jsyh"];
            }else if ([bankNameStr rangeOfString:@"工商银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gsyh"];
            }else if ([bankNameStr rangeOfString:@"农业银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_nyyh"];
            }else if ([bankNameStr rangeOfString:@"中国银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zgyh"];
            }else if ([bankNameStr rangeOfString:@"交通银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_jtyh"];
            }else if ([bankNameStr rangeOfString:@"兴业银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_xyyh"];
            }else if ([bankNameStr rangeOfString:@"中信银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zxyh"];
            }else if ([bankNameStr rangeOfString:@"光大银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gdyh"];
            }else if ([bankNameStr rangeOfString:@"平安银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_payh"];
            }else if ([bankNameStr rangeOfString:@"邮政银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_yzyh"];
            }else if ([bankNameStr rangeOfString:@"上海银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_shyh"];
            }else if ([bankNameStr rangeOfString:@"浦发银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_pfyh"];
            }else if ([bankNameStr rangeOfString:@"民生银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_msyh"];
            }else if ([bankNameStr rangeOfString:@"招商银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zsyh"];
            }else if ([bankNameStr rangeOfString:@"广发银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gfyh"];
            }else if ([bankNameStr rangeOfString:@"上海农商银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_shnsyh"];
            }else if ([bankNameStr rangeOfString:@"北京银行"].length != 0) {
                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_bjyh"];
            }

        return cell;}
    
//        }else{
//        LimitTableCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellID];
//        if (cell == nil) {
//            cell = [[NSBundle mainBundle] loadNibNamed:@"LimitTableCell" owner:self options:nil][0];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        }
//            NSString *bankNameStr = _dataArray[indexPath.row];
//            cell.bankNameLabel.text = bankNameStr;
//            
//            if ([bankNameStr rangeOfString:@"建设银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_jsyh"];
//            }else if ([bankNameStr rangeOfString:@"工商银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gsyh"];
//            }else if ([bankNameStr rangeOfString:@"农业银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_nyyh"];
//            }else if ([bankNameStr rangeOfString:@"中国银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zgyh"];
//            }else if ([bankNameStr rangeOfString:@"交通银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_jtyh"];
//            }else if ([bankNameStr rangeOfString:@"兴业银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_xyyh"];
//            }else if ([bankNameStr rangeOfString:@"中信银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zxyh"];
//            }else if ([bankNameStr rangeOfString:@"光大银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gdyh"];
//            }else if ([bankNameStr rangeOfString:@"平安银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_payh"];
//            }else if ([bankNameStr rangeOfString:@"邮政银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_yzyh"];
//            }else if ([bankNameStr rangeOfString:@"上海银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_shyh"];
//            }else if ([bankNameStr rangeOfString:@"浦发银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_pfyh"];
//            }else if ([bankNameStr rangeOfString:@"民生银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_msyh"];
//            }else if ([bankNameStr rangeOfString:@"招商银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zsyh"];
//            }else if ([bankNameStr rangeOfString:@"广发银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gfyh"];
//            }else if ([bankNameStr rangeOfString:@"上海农商银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_shnsyh"];
//            }else if ([bankNameStr rangeOfString:@"北京银行"].length != 0) {
//                cell.logoImageView.image = [UIImage imageNamed:@"BankLogo_bjyh"];
//            }
//            
//            NSString *mindStr = @"";
//            
//            if ([_isLowArray[indexPath.row] isEqualToString:@"1"]) {
//                if ([_isOpenBankArray[indexPath.row] isEqualToString:@"1"]) {
//                    mindStr = @"*单笔单日额度较低不建议使用*需开通银联在线支付http://online.unionpay.com/static/open/";
//                }else {
//                    mindStr = @"*单笔单日额度较低不建议使用";
//                }
//            }else if ([_isLowArray[indexPath.row] isEqualToString:@"2"]) {
//                mindStr = @"近期单笔最高2万，单日最高2万";
//            }else {
//                if ([_isOpenBankArray[indexPath.row] isEqualToString:@"1"]) {
//                    if ([bankNameStr rangeOfString:@"浦发银行"].length != 0) {
//                        mindStr = @"*需在浦发网上银行内点击开通银联在线支付";
//                    }else {
//                        mindStr = @"*需开通银联在线支付http://online.unionpay.com/static/open/";
//                    }
//                }else {
//                    mindStr = @"";
//                }
//            }
//            CGFloat mindHeight = 0.0f;
//            if (mindStr.length <= 0) {
//                cell.mindLabel.hidden = YES;
//                mindHeight = KAdaptNum(18);
//            }else {
//                cell.mindLabel.hidden = NO;
//                mindHeight = [MyTools getSizeOfString:mindStr maxWidth:MSWIDTH - 10 - KAdaptNum(115) maxHeight:1000 withFontSize:10].height;
//                if (mindHeight < KAdaptNum(18)) {
//                    mindHeight = KAdaptNum(18);
//                }
//            }
//            cell.bankNameLabel.frame = RECTMAKE(KAdaptNum(50), KAdaptNum(8), KAdaptNum(65), KAdaptNum(18));
//            cell.mindLabel.frame = RECTMAKE(GetMaxX(cell.bankNameLabel), KAdaptNum(10), MSWIDTH - 10 - GetMaxX(cell.bankNameLabel), mindHeight);
//            cell.mindLabel.text = mindStr;
//            cell.limitAmountLabel.frame = RECTMAKE(KAdaptNum(50), GetMaxY(cell.mindLabel), MSWIDTH - 10 - KAdaptNum(50), KAdaptNum(20));
//            cell.logoImageView.frame = RECTMAKE(KAdaptNum(10), (GetMaxY(cell.limitAmountLabel) + 5 - KAdaptNum(30))/2, KAdaptNum(30), KAdaptNum(30));
//            
//            NSString *limitStrrrrr = _limitArray[indexPath.row];
//            if (limitStrrrrr.doubleValue >= 10000) {
//                limitStrrrrr = [NSString stringWithFormat:@"%.0f万",limitStrrrrr.doubleValue/10000.0];
//            }
//            NSString *dayLimitStrrrrr = _dayLimitArray[indexPath.row];
//            if (dayLimitStrrrrr.doubleValue >= 10000) {
//                dayLimitStrrrrr = [NSString stringWithFormat:@"%.0f万",dayLimitStrrrrr.doubleValue/10000.0];
//            }
//            NSString *oncelimitStr = [NSString stringWithFormat:@"单笔最高%@,",limitStrrrrr];
//            NSString *dayLimitStr = [NSString stringWithFormat:@"单日最高%@",dayLimitStrrrrr];
//            NSString *limitStr = [NSString stringWithFormat:@"%@%@",oncelimitStr,dayLimitStr];
//            
//            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:limitStr];
//            [attributedString addAttribute:NSFontAttributeName value:KFontSize(10) range:NSMakeRange(4, oncelimitStr.length-1-4)];
//            [attributedString addAttribute:NSFontAttributeName value:KFontSize(10) range:NSMakeRange(oncelimitStr.length+4, limitStr.length-oncelimitStr.length-4)];
//            [attributedString addAttribute:NSForegroundColorAttributeName value:KColor range:NSMakeRange(4, oncelimitStr.length-1-4)];
//            [attributedString addAttribute:NSForegroundColorAttributeName value:KColor range:NSMakeRange(oncelimitStr.length+4, limitStr.length-oncelimitStr.length-4)];
//            cell.limitAmountLabel.attributedText = attributedString;
//            cell.limitAmountLabel.adjustsFontSizeToFitWidth = YES;
//            
//            cell.frame=CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, GetMaxY(cell.limitAmountLabel) + 5);
//            return cell;
//
//        }
            else {
        EmptyTableViewCell *cell = [[EmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"emptyCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor = KbackgroundColor;
        
        cell.frame=CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, _tableView.frame.size.height);
        return cell;
    }

    }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height+30;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
